#!/bin/bash
############################################################################
# Wikiquote-Twitt
# Ver 0.0.2
# (C) Copyleft 2011 Carlos Amorim / porele.blog<anfora>gmail.com
# http://porele.wordpress.com/
# Usage: ./wikiquotetw
############################################################################

TMP=${TMP:-/tmp}
CURRENTFILE=$(readlink -f $0)
CURRENTPATH=$(dirname $CURRENTFILE)
PROJECTPATH=`cd $CURRENTPATH; cd ..; pwd` # Obtém o path absoluto do projeto
QUOTES=$TMP/quotes$$ # quotes já no formato em que serão twitadas
QUOTESRC=$PROJECTPATH/res/quotesources.txt # arquivo contendo url_wikiquote|nome_do_pensador
QUOTESRCAUX=$TMP/quotesources.txt$$ # arquivo de quotes sem as linhas comentadas
IMAGEPATH=$PROJECTPATH/img/defaultlogo.png

# remove as linhas comentadas
sed '/^ *#/d;s/#.*//' $QUOTESRC > $QUOTESRCAUX

# obtém a partir das URLs da Wikiquote as frases dos pensadores
while read LINE
do
	URL=`echo $LINE | awk -F '|' '{print $1}'`
	NOME=`echo $LINE | awk -F '|' '{print $2}'`
	w3m -cols 120 -dump $URL | grep "^[[:space:]]\{2\}[[:punct:]].*\.\"$" | sed "s/^[[:space:]]\{2\}[[:punct:]]//g" | xargs -n1 | sed s/.$/.$" ($NOME)"/ >> $QUOTES
done < $QUOTESRCAUX

# twita as frases anteriormente obtidas
while read LINE
do
	ttytter -status="$LINE"
	notify-send "Twitter" "$LINE" -i $IMAGEPATH
	sleep 10m	# tempo entre as postagens. 10s = 10 segundos e 10m = 10 minutos
done < $QUOTES

rm -f $QUOTES
rm -f $QUOTESRCAUX
